package com.gxa.day2022_08_09_28.emp_dept_system.service.impl;



import com.gxa.day2022_08_09_28.emp_dept_system.dao.DeptDao;
import com.gxa.day2022_08_09_28.emp_dept_system.dao.EmpDao;
import com.gxa.day2022_08_09_28.emp_dept_system.dao.impl.DeptDaoImpl;
import com.gxa.day2022_08_09_28.emp_dept_system.dao.impl.EmpDaoImpl;
import com.gxa.day2022_08_09_28.emp_dept_system.entity.Dept;
import com.gxa.day2022_08_09_28.emp_dept_system.entity.Emp;
import com.gxa.day2022_08_09_28.emp_dept_system.service.EmpService;

import java.util.List;
import java.util.Vector;

public class EmpServiceImpl implements EmpService {

    @Override
    public Object[][] queryAll() {
        EmpDao empDao = new EmpDaoImpl();
        List<Emp> emps = empDao.queryAll();

        Object[][] objects = list2Array(emps);
        return objects;
    }

    @Override
    public void add(Emp emp) {
        EmpDao empDao = new EmpDaoImpl();
        empDao.save(emp);
    }

    @Override
    public Emp getById(Integer id) {
        EmpDao empDao = new EmpDaoImpl();
        Emp emp = empDao.getById(id);
        return emp;
    }

    @Override
    public void update(Emp emp) {
        EmpDao empDao = new EmpDaoImpl();
        empDao.update(emp);
    }

    @Override
    public void deleteById(Integer id) {
        EmpDao empDao = new EmpDaoImpl();
        empDao.delete(id);
    }

    @Override
    public Vector<Dept> queryDepts() {
        DeptDao deptDao = new DeptDaoImpl();
        Vector<Dept> depts = deptDao.queryAll();
        return depts;
    }

    private Object[][] list2Array(List<Emp> emps){
        Object[][] data = new Object[emps.size()][8];
        for(int i=0;i<emps.size();i++){
            Emp emp = emps.get(i);

            data[i][0] = emp.getEmpno();
            data[i][1] = emp.getEname();
            data[i][2] = emp.getJob();
            data[i][3] = emp.getMgr();
            data[i][4] = emp.getHireDate();
            data[i][5] = emp.getSal();
            data[i][6] = emp.getComm();
            data[i][7] = emp.getDept().getDname();

        }

        return data;
    }
}
